package com.chat.view;

import com.chat.Local;
import com.chat.service.DisbandGroup;
import com.chat.service.ExitGroup;
import com.chat.service.GetGroupMessage;
import com.chat.io.SetCircle;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

import java.io.File;
import java.io.IOException;

public class GroupMsgController {
    public ImageView touxiang;
    public Label id;
    public Label name;
    public Label number;
    public Label time;
    public Label info;
    public Button disbandbutton;
    public Button exitbutton;
    public Button xiugaibutton;
    public Button yanzhengbutton;


    public void initialize() {
         disbandbutton.setVisible(false);
         exitbutton.setVisible(false);
         xiugaibutton.setVisible(false);
         yanzhengbutton.setVisible(false);
        {
            GetGroupMessage ggm = new GetGroupMessage();
            ggm.getgroupmessage(Local.group.getId());
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            id.setText(Local.group.getId());
            name.setText(Local.group.getName());
            number.setText(Local.group.getCount());
            time.setText(Local.group.getTime());
            info.setText(Local.group.getInfo());
            File file1 = new File("D:/chatproject/client/" + Local.group.getId() + ".png");
            touxiang.setImage(new Image("file:" + file1.getAbsolutePath()));
            touxiang.setClip(new SetCircle().getRectangle(40,40));
        }//初始化群消息
        {
            if (Local.user.getUsername().equals(Local.group.getGroupleader())) {
                disbandbutton.setVisible(true);
                xiugaibutton.setVisible(true);
                yanzhengbutton.setVisible(true);
            } else exitbutton.setVisible(true);
        }//根据不同的身份隐藏不同的按钮
    }

    public void xiugai(){//修改群信息
        Stage stage=(Stage) xiugaibutton.getScene().getWindow();
        stage.close();
        try {
            ChangeGroupMsgView.start(new Stage());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }//修改资料按钮的点击事件

    public void disband(){//解散群聊
        DisbandGroup dg=new DisbandGroup();
        dg.disbandgroup(Local.group.getId());
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("提示");
        alert.setHeaderText("恭喜");
        alert.setContentText("解散成功！");
        alert.showAndWait();
        fanhui();
    }//解散群聊按钮的点击事件

    public void exit(){//退出群聊
        ExitGroup eg=new ExitGroup();
        eg.exitgroup(Local.group.getId(),Local.user.getUsername());
        fanhui();
    }//退出群聊按钮的点击事件

    public void groupmember(){//查看群成员
        Stage stage=(Stage) xiugaibutton.getScene().getWindow();
        stage.close();
        try {
            GroupMemberView.start(new Stage());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void fanhui() {
               Stage stage=(Stage) xiugaibutton.getScene().getWindow();
               stage.close();
        try {
            MainWindowView.start(new Stage());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }//返回主界面

    public void yanzheng(MouseEvent mouseEvent) {
        try {
            Stage stage=(Stage) xiugaibutton.getScene().getWindow();
            stage.close();
            GroupVerifyView.start(new Stage());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }//打开验证消息界面
}
